<template>
  <grid title="个人设置">
    <el-form ref="form"
             style="text-align: left; width:1000px"
             label-width="100px"
             @submit.native.prevent="onFormSubmit"
             :model="form"
             :rules="rules">
      <el-form-item label="留言">
        <el-checkbox v-model="form.message">开启</el-checkbox>
        <p class="caption">开启时将允许用户留言</p>
      </el-form-item>
      <el-form-item label="评论">
        <el-checkbox v-model="form.comment">开启</el-checkbox>
        <p class="caption">开启时将允许用户评论</p>
      </el-form-item>
      <el-form-item label="聊天">
        <el-checkbox v-model="form.chat">开启</el-checkbox>
        <p class="caption">开启时将展示聊天板块</p>
      </el-form-item>
      <el-form-item label="打赏">
        <el-checkbox v-model="form.reward">开启</el-checkbox>
        <p class="caption">开启时将在文章详情页显示打赏功能</p>
      </el-form-item>
      <template v-if="form.reward">
        <el-form-item label="支付宝"
                      prop="alipay">
          <image-input type="other"
                       v-model="form.alipay"></image-input>
        </el-form-item>
        <el-form-item label="微信"
                      prop="wechat">
          <image-input type="other"
                       v-model="form.wechat"></image-input>
        </el-form-item>
      </template>
      <el-form-item>
        <el-button type="primary"
                   native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </grid>
</template>

<script>
import imageInput from '@/components/image-input'
import { mapActions, mapGetters } from 'vuex'
import { deepCopy } from '@/utils/common'

export default {
  title () {
    return '个人设置'
  },
  data () {
    const validateCode = (rule, value, callback) => {
      if (this.form.reward === 1 && value.length === 0) {
        callback(new Error('开启打赏后必须打赏上传二维码'))
      } else {
        callback()
      }
    }
    return {
      form: {
        message: false,
        comment: false,
        chat: false,
        reward: false,
        alipay: [],
        wechat: []
      },
      rules: {
        alipay: [
          { validator: validateCode }
        ],
        wechat: [
          { validator: validateCode }
        ]
      }
    }
  },
  components: {
    imageInput
  },
  async asyncData ({ store, route }) {
    const { query } = route;
    return store.dispatch('setting/view', 'module')
  },
  computed: {
    ...mapGetters('setting', ['module'])
  },
  async mounted () {
    this.$nextTick(() => {
      this.form = deepCopy(this.module)
      this.form.alipay = this.form.alipay !== '{}' ? [this.form.alipay] : []
      this.form.wechat = this.form.wechat !== '{}' ? [this.form.wechat] : []
    })
  },
  methods: {
    ...mapActions('setting', ['updateModule']),
    async onFormSubmit () {
      await this.$refs.form.validate()
      let alipay = {}
      let wechat = {}
      if (this.form.alipay.length) {
        const { alipay: [{ id, url }] } = this.form
        alipay = { id, url }
      }
      if (this.form.wechat.length) {
        const { wechat: [{ id, url }] } = this.form
        wechat = { id, url }
      }
      const params = { ...this.form, alipay, wechat }
      await this.updateModule(params)
      this.$message.success('保存成功')
    }
  }
}
</script>
